<?php
/**
 * This file is part of the Agora distribution. 
 * Detailed copyright and licensing information can be found
 * in the gpl-3.0.txt file which should be included in the distribution.
 * 
 * @version		$Id: 3.0.14 Olympus 2010-09-28 rammstein4o $
 * @copyright  2007 - 2010 jVitals
 * @license   GPLv3 Open Source
 * @link       http://jvitals.com
 * @since      File available since initial release
 */

defined ('IN_AGORA') or die;

class TaskController extends AgoraCommonController
{
    function _default()
    {

    }
	
	function addrecipient()
	{
		$jsonDATA = array();
		$html = '';
		$added = false;
		$count = 0 ;
		$user_model = & Model::getInstance('UserModel');
		
		$id = intval(Agora::getVar("id"));
		$session =& JFactory::getSession();
		$recipients = $session->get('agora_recipient', NULL , 'agora');
		
		if (isset($recipients) && is_array($recipients)) {
			$rev = array_reverse($recipients);
			$count = count($rev);
			foreach ($rev as $uid)
			{
				$user = $user_model->load($uid);
				$use_name = ($this->agora_config['o_fullnames_enabled'] ? $user['name'] : $user['username']);
				$html .= '<div class="pms_remove" onclick="remFromPM(\''. $uid . '\')" >'.
							$use_name .' <img src="'.Agora::image('buttons/delete.png').'" width="16" height="16" /></div>';
			}
			
		}
		if ($count < 5) {
			$user = $user_model->load($id);
			$use_name = ($this->agora_config['o_fullnames_enabled'] ? $user['name'] : $user['username']);
			$recipients[] = $id;
			$count ++; 
			$msg =  '<div class="pms_remove" onclick="remFromPM(\''. $id . '\')" >'.
					$use_name .' <img src="'.Agora::image('buttons/delete.png').'" width="16" height="16" /></div>';
			$added = true;
		}
		else {
			$msg =  '<img src="'.Agora::image('icons/warning.png').'" width="16" height="16" /> '. Agora::lang('Max Reached'). ' - [<b>'. $count . ' of 5</b>]';
		}
		
		$session->set('agora_recipient', $recipients , 'agora');

		$reset = '<span class="pms_link" onclick="resetPM()" >'.
		Agora::lang('Clear All ') .'</span>';
		$continue = '<span class="pms_link" onclick="sendPM()" >'.
		Agora::lang('Continue ') .'</span>';
		$jsonDATA['html'] = $html ;
		$jsonDATA['msg'] = $msg ;
		$jsonDATA['links'] ='<div class="ag_rightend">
								<h2 class="pms_linkbar">
									<span class="ag_middle">'.$reset.' '. $continue. '</span>
								</h2>
							</div>' ;
		$jsonDATA['title'] = Agora::lang('Message Recipients') . ' - [<b>'. $count . ' '. Agora::lang('msgof') . ' 5</b>]';
		$jsonDATA['res'] = $added;
		echo json_encode($jsonDATA);
		//return;
		
	}
	
	function reset()
	{
		$jsonDATA = array();
		$html = Agora::lang('Choose Recipients');
		$msg = Agora::lang('Reset');
		$session =& JFactory::getSession();
		$session->set('agora_recipient', null , 'agora');

		$jsonDATA['html'] = $html ;
		$jsonDATA['msg'] = $msg ;
		$jsonDATA['links'] = '';
		$jsonDATA['title'] = Agora::lang('Message Recipients') . ' - [<b>0 '. Agora::lang('msgof') . ' 5</b>]';
		echo json_encode($jsonDATA);
		//return;
	}
	function remrecipient()
	{
		$jsonDATA = array();
		$html = '';
		$added = false;
		$count = 0 ;
		$user_model = & Model::getInstance('UserModel');
		
		$id = intval(Agora::getVar("id"));
		
		$session =& JFactory::getSession();
		$recipients = $session->get('agora_recipient', NULL , 'agora');
		
		if (isset($recipients) && is_array($recipients)) {
			$count = count($recipients);
			foreach ($recipients as $uid_key => $uid)
			{
				if ($id == intval($uid)) unset($recipients[$uid_key]);
			}
			$recipients = array_values($recipients);
			foreach ($recipients as $uid)
			{
				$user = $user_model->load($uid);
				$use_name = ($this->agora_config['o_fullnames_enabled'] ? $user['name'] : $user['username']);
				$html .= '<div class="pms_remove" onclick="remFromPM(\''. $uid . '\')" >'.
		$use_name .' <img src="'.Agora::image('buttons/delete.png').'" width="16" height="16" /></div>';
			}
			
		}
				
		$session->set('agora_recipient', $recipients , 'agora');

		$reset = '<span class="pms_link" onclick="resetPM()" >'.
		Agora::lang('Clear All ') .'</span>';
		$continue = '<span class="pms_link" onclick="sendPM()" >'.
		Agora::lang('Continue ') .'</span>';
		$jsonDATA['html'] = $html ;
		$jsonDATA['msg'] = '' ;
		$jsonDATA['links'] ='<div class="ag_rightend">
								<h2 class="pms_linkbar">
									<span class="ag_middle">'.$reset.' '. $continue. '</span>
								</h2>
							</div>' ;
		$jsonDATA['title'] = Agora::lang('Message Recipients') . ' - [<b>'. $count . ' '. Agora::lang('msgof') . ' 5</b>]';
		$jsonDATA['res'] = $added;
		echo json_encode($jsonDATA);
	}
	
	function update()
	{
		$jsonDATA = array();
		$html = '';
		$added = false;
		$count = 0 ;
		$user_model = & Model::getInstance('UserModel');
		$session =& JFactory::getSession();
		$recipients = $session->get('agora_recipient', NULL , 'agora');
		
		if (isset($recipients) && is_array($recipients)) {
			$rev = array_reverse($recipients);
			$count = count($rev);
			foreach ($rev as $uid)
			{
				$user = $user_model->load($uid);
				$use_name = ($this->agora_config['o_fullnames_enabled'] ? $user['name'] : $user['username']);
				$html .= '<div class="pms_remove" onclick="remFromPM(\''. $uid . '\')" >'.
					$use_name .' <img src="'.Agora::image('buttons/delete.png').'" width="16" height="16" /></div>';
			}
			$reset = '<span class="pms_link" onclick="resetPM()" >'.
				Agora::lang('Clear All') .'</span>';
			$continue = ' <span class="pms_link" onclick="sendPM()" >'.
				Agora::lang('Continue') .'</span>';
			$title = 'Recipients'. ' - [<b>'. $count . ' of 5</b>]';
			$jsonDATA['links'] = '<div class="ag_rightend">
									<h2 class="pms_linkbar">
										<span class="ag_middle">'.$reset.' '. $continue. '</span>
									</h2>
								</div>' ;
		}
		else {
			$reset = '';
			$continue = '';
			$html = Agora::lang('Choose Recipients');
			$title = Agora::lang('Message Recipients') . ' - [<b>0 '. Agora::lang('msgof') . ' 5</b>]';
			$jsonDATA['links'] = '';
		}
		$jsonDATA['html'] = $html ;
		$jsonDATA['msg'] = '' ;
		$jsonDATA['title'] = $title;
		$jsonDATA['res'] = $added;
		echo json_encode($jsonDATA);
		//return;
	}
	
	function search() 
	{
		$user_model = & Model::getInstance('UserModel');
		$q 			= JRequest::getVar( 'q' );
		$searchq	=	strip_tags($q);
		$l = strlen($searchq);
		if ($l < 2) {
			die;
		}
		
		$users = $user_model->ajaxSearch($this->agora_config['o_fullnames_enabled']);
		if ($users != NULL )
		{
			$html = '<ul>';
			foreach ($users as $user) {
				$use_name = ($this->agora_config['o_fullnames_enabled'] ? $user->name : $user->username);
				$html .= '<li><a href="javascript:void(0);" onclick="addSearchToPM(\''.$user->id.'\') " >'.$use_name.'</a></li>';
				//$html .= ''.$user->username .'<small>'. $user->id '.</small>';
			}
			$html .= '</ul>';
		}
		else
		{
			$html = JText::_( 'No Results' );
		}
		echo $html;
 die;
 }
}
?>
